/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package frontController;

import businessDelegates.AdminBusinessDelegate;
import businessDelegates.ClienteBusinessDelegate;
import businessDelegates.FormularioBusinessDelegate;
import businessObjects.Admin;
import businessObjects.Cliente;
import businessObjects.Formulario;
import daos.AdminDAO;
import daos.exceptions.PreexistingEntityException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author williamprado
 */
@WebServlet(name="FrontController", urlPatterns={"/FrontController"})
public class FrontController extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

        AdminBusinessDelegate abd = new AdminBusinessDelegate();
        ClienteBusinessDelegate cli= new ClienteBusinessDelegate();

        
        String login= request.getParameter("login");
        String pass= request.getParameter("pass");
        String tipo= request.getParameter("tipousuario");
        System.out.println (login);
        System.out.println (pass);

        if(tipo=="0"){


        Admin c = new Admin();
        //c.setIdAdmin(0);
       String loginbd=c.getLogin();
       String passbd=c.getPassword();

        if(loginbd==login && passbd==pass)

//        c.setLogin("testadn");
        //c.setNombre("name");
 //       c.setPassword("pas");
        try {
            //AdminDAO ad = new AdminDAO();
  //          abd.registrarAdmin(c);
            response.sendRedirect("basicads.jsp");
        //} catch (PreexistingEntityException ex) {
          //  Logger.getLogger(FrontController.class.getName()).log(Level.SEVERE, null, ex);
            //response.sendRedirect("error.jsp");
        } catch (Exception ex) {
            Logger.getLogger(FrontController.class.getName()).log(Level.SEVERE, null, ex);
            response.sendRedirect("error.jsp");
        }
        }//new ClienteBusinessDelegate().registrarCliente(c);
        
        if(tipo=="1"){
       Cliente p = new Cliente();
        //c.setIdAdmin(0);
  //      p.setLogin("testadn");
        //c.setNombre("name");
  //      p.setPassword("pas");

            String loginbd=p.getLogin();
       String passbd=p.getPassword();

        if(loginbd==login && passbd==pass)
        try {
            //AdminDAO ad = new AdminDAO();
            cli.registrarCliente(p);
            response.sendRedirect("basicads.jsp");
        //} catch (PreexistingEntityException ex) {
          //  Logger.getLogger(FrontController.class.getName()).log(Level.SEVERE, null, ex);
            //response.sendRedirect("error.jsp");
        } catch (Exception ex) {
            Logger.getLogger(FrontController.class.getName()).log(Level.SEVERE, null, ex);
            response.sendRedirect("error.jsp");
        }
        }
        
        FormularioBusinessDelegate FormBusinessDelegate= new FormularioBusinessDelegate();
 //       FormBusinessDelegate.
        Formulario Formulario= new Formulario();
  //      Formulario.

        String nombre= request.getParameter("nombre");
        String email= request.getParameter("email");
        String telefono= request.getParameter("telefono");
        String direccion= request.getParameter("direccion");
        String pais= request.getParameter("pais");
        String ciudad= request.getParameter("ciudad");


        //new ClienteBusinessDele
/*
        // Create ApplicationController for handling incoming request
       ApplicationControllerFactory ACFactory = ApplicationControllerFactory.getInstance();
       ApplicationController applicationController = ACFactory.getApplicationController(request);
       applicationController.init();

       // Create ContextObject to encapsulate protocol-specific
       // request state
       RequestContextFactory requestContextFactory = RequestContextFactory.getInstance();
       RequestContext requestContext = requestContextFactory.getRequestContext(request);

       // Action Management - Locate & Invoke Actions to handle specific
       // Requests
       ResponseContext responseContext;
       responseContext = applicationController.handleRequest(requestContext);
       responseContext.setResponse(response);

       // View Management - Navigate and Dispatch to appropriate View
       applicationController.handleResponse(requestContext, responseContext);
       applicationController.destroy();*/


        //response.setContentType("text/html;charset=UTF-8");
        //PrintWriter out = response.getWriter();
        //try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet FrontController</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet FrontController at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
            */
        //} finally {
            //out.close();
        //}
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
